/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: 1.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月08日 星期四 11时02分32秒
***************************************************/
//定义一个全局变量，创建两个线程，对这个全局变量实现加1操作
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t mutex;
int data = 0;
void*fun1(void *arg){
	while(1){
	pthread_mutex_lock(&mutex);
	data++;
	printf("fun1:%d\n",data);
	pthread_mutex_unlock(&mutex);
	
	sleep(1);
}

}
void*fun2(void *arg){
	while(1){
	pthread_mutex_lock(&mutex);
	data++;
	printf("fun2:%d\n",data);
	pthread_mutex_unlock(&mutex);
	
	sleep(1);
}
}
int main(int argc,const char* argv[])
{
	pthread_t tid1 = 0,tid2 = 0;
	int ret = 0;
	pthread_mutex_init(&mutex,NULL);
	ret = pthread_create(&tid1,NULL,fun1,NULL);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(-1);
	}
	ret = pthread_create(&tid2,NULL,fun2,NULL);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(-1);
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	pthread_mutex_destroy(&mutex);
	
    return 0;
}
